package com.itheima.servlet;

import com.itheima.mapper.UserMapper;
import com.itheima.trueclass.User;
import com.itheima.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;
import sun.text.normalizer.UTF16;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

//第一步继承HttpServlet
    //注解设置这个网页的地址
@WebServlet("/myWeb")
public class OneHTTP extends HttpServlet {


    //重写doGet和doPost方法
    @Override//处理get的提交方法
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       /* //1,设置网页的行内容  响应类型和编码格式
        response.setContentType("text/html; charset=UTF-8");
        //2,获得向网页输出的打印流
        PrintWriter respWriter = response.getWriter();
        //3,向网页发送
        respWriter.println("<html>\n" +
                "  <button type=\"button\" id=\"but\" onclick=\"addHefty()\">添加黄网链接</button>\n" +
                "    <a target=\"_blank\">黄色网站</a>\n" +
                "  <div>\n" +
                "<img src='https://tse4-mm.cn.bing.net/th/id/OIP-C.K8sXVnGGddUZsHE8HZlAkgHaK_?pid=ImgDet&rs=1'/>" +
                "  </div>\n" +
                "</body>\n" +
                "</html>\n" +
                "<script>\n" +
                "  function addHefty() {\n" +
                "    let line = document.getElementsByTagName(\"a\");\n" +
                "    for (let lineElement of line) {\n" +
                "      lineElement.href = \"https://baike.baidu.com/item/%E9%BB%84%E8%89%B2%E7%BD%91%E7%AB%99/389303?fr=aladdin\";\n" +
                "    }\n" +
                "  }\n" +
                "</script>");*/
        //System.out.println("地址:" + request.getRequestURL());
        //使用mybatis验证密码的正确性
        SqlSession session = MybatisUtil.getSession();
        String user = request.getParameter("user");
        String passWord = request.getParameter("passWord");
        User user1 = session.getMapper(UserMapper.class).login(user, passWord);
        if(user1!=null){
            System.out.println("登录成功");
        }else {
            System.out.println("登录失败");
        }

/*        System.out.println("用户名:"  + user);
        System.out.println("密码:"  + passWord);*/
    }

    @Override//处理post的提交方法
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }
}
